//
//  CHTopTextView.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/7.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHTopTextView.h"
@interface CHTopTextView ()<UITextViewDelegate>
@property (nonatomic ,strong) UIButton *sendBtn;
@property (nonatomic ,strong) UIView *bgView;
@property (nonatomic ,strong) UILabel *placeholderLabel;
@end;
@implementation CHTopTextView

+(instancetype) topTextView{
    
    return [[self alloc] init];
}


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
     
        self.frame = CGRectMake(0, UIScreenH, UIScreenW, 120);
        //        self.lpTextView.scrollsToTop = NO;
        self.backgroundColor = KRGB(242, 242, 242);
        [self setupUI];
        // 添加键盘监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}


-(void)setupUI{
    self.textView = [[UITextView alloc] init];
    // 输入框
    self.textView.frame = CGRectMake(10, 40, UIScreenW - 20, 120 - 40-10);
    self.textView.font = [UIFont systemFontOfSize:14];
    self.textView.layer.borderWidth = 0.5;
    self.textView.delegate = self;
    self.textView.layer.borderColor = [UIColor colorWithHexString:@"999999" alpha:1.0f].CGColor;
    self.textView.layer.cornerRadius = 2;
    self.textView.clipsToBounds = YES;
    [self addSubview:self.textView];
    
    
    UILabel *placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, self.textView.w - 10, 25)];
    placeholderLabel.font = [UIFont systemFontOfSize:14];
    placeholderLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:0.5f];
    placeholderLabel.text = @"请输入拒绝的原因...";
    [self.textView addSubview:placeholderLabel];
    self.placeholderLabel = placeholderLabel;
    
    
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 40)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithHexString:@"999999" alpha:1.0f] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    UIButton *refuseBtn = [[UIButton alloc] initWithFrame:CGRectMake(UIScreenW - 40 -10, 5, 40, 40)];
    [refuseBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    [refuseBtn setTitleColor:[UIColor colorWithHexString:@"999999" alpha:1.0f] forState:UIControlStateNormal];
    refuseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [refuseBtn addTarget:self action:@selector(refuseBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:refuseBtn];
    
    
//    UILabel *causeLabel = [[UILabel alloc] initWithFrame:CGRectMake((UIScreenW - 60) /2, 5, 60, 40)];
//    causeLabel.text = @"原因";
//    causeLabel.textAlignment = NSTextAlignmentCenter;
//    causeLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
//    causeLabel.font = [UIFont systemFontOfSize:14];
//    [self addSubview:causeLabel];
    

    // 半透明灰色背景
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH)];
    _bgView.backgroundColor = [UIColor colorWithHexString:@"0x000000" alpha:1.0f];
    _bgView.alpha = 0.5;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardWillDisappear)];
    [_bgView addGestureRecognizer:tap];
    
    
}




-(void)textViewDidChange:(UITextView *)textView{
    
    self.placeholderLabel.hidden = self.textView.text.length >0 ? YES :NO;
    
}



-(void)refuseBtnClick{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(sendCommentWithRefuseText:)]) {
        [self.delegate sendCommentWithRefuseText:self.textView.text];
    }
    [self.textView resignFirstResponder];
    
}

//  取消
-(void)cancelBtnClick{
    
    [self.textView resignFirstResponder];
    
}

#pragma mark - 监听键盘
- (void)keyboardWillAppear:(NSNotification *)notif
{
    
    if ([self.textView isFirstResponder]) {
        NSDictionary *info = [notif userInfo];
        NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGSize keyboardSize = [value CGRectValue].size;
//        NSLog(@"keyboardSize.height%f",keyboardSize.height);

        [UIView animateWithDuration:0.3 animations:^{

          self.y = UIScreenH - keyboardSize.height - 120 ;

        }];
        [self.superview addSubview:_bgView];
        [self.superview addSubview:self];
    }
}
- (void)keyboardWillDisappear:(NSNotification *)notif
{
    
    [UIView animateWithDuration:0.5 animations:^{
        self.y = UIScreenH;
    }];
    [_bgView removeFromSuperview];
}

#pragma mark - 非通知调用键盘消失方法
- (void)keyboardWillDisappear
{
    [self.textView resignFirstResponder];
}



-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
